﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Twodi.Behavior.Interfaces;
using Twodi.Interfaces;
using Twodi.Aspect.Args;

namespace Twodi.Implemented
{
    public enum TooltipAnchor
    {
        Free,
        Top_Left,
        Top_Center,
        Top_Right,
        Middle_Left,
        Middle_Right,
        Bottom_Left,
        Bottom_Center,
        Bottom_Right
    }

    public class TooltipComponent : Label
    {
        protected ITooltipTarget target;

        protected TooltipAnchor anchor = TooltipAnchor.Free;

        public TooltipComponent(ITooltipTarget target, SpriteFont font, Game game)
            : base(font, game)
        {
            this.Visible = false;

            this.Target = target;

            Target.MouseBehavior.MouseOver += (sender, e) =>
            {
                this.Visible = true;
                return true;
            };
            Target.MouseBehavior.MouseOut += (sender, e) =>
            {
                this.Visible = false;
                return true;
            };

            Target.PositionProperties.PositionChanged += new EventHandler<PositionChangedEventArgs>(UpdateTooltipPosition);
            AnchorChanged += new EventHandler<EventArgs>(UpdateTooltipPosition);
            TargetChanged += new EventHandler<EventArgs>(UpdateTooltipPosition);
        }

        void UpdateTooltipPosition(object sender, EventArgs e)
        {
            DefinePosition();
        }

        protected virtual void DefinePosition()
        {
            switch (Anchor)
            {
                case TooltipAnchor.Top_Left:
                    this.Position = new Vector2(Target.PositionProperties.Position.X - Size.X, Target.PositionProperties.Position.Y - Size.Y);
                    break;
                case TooltipAnchor.Top_Center:
                    this.Position = new Vector2(Target.PositionProperties.Position.X - Math.Abs((Target.SizeProperties.Size.X / 2 - Size.X / 2)), Target.PositionProperties.Position.Y - Size.Y);
                    break;
                case TooltipAnchor.Top_Right:
                    this.Position = new Vector2(Target.PositionProperties.Position.X + Target.SizeProperties.Size.X, Target.PositionProperties.Position.Y - Size.Y);
                    break;

                case TooltipAnchor.Middle_Left:
                    this.Position = new Vector2(Target.PositionProperties.Position.X - Size.X, Target.PositionProperties.Position.Y + Math.Abs((Target.SizeProperties.Size.Y / 2 - Size.Y / 2)));
                    break;
                case TooltipAnchor.Middle_Right:
                    this.Position = new Vector2(Target.PositionProperties.Position.X + Target.SizeProperties.Size.X, Target.PositionProperties.Position.Y + Math.Abs((Target.SizeProperties.Size.Y / 2 - Size.Y / 2)));
                    break;

                case TooltipAnchor.Bottom_Left:
                    this.Position = new Vector2(Target.PositionProperties.Position.X - Size.X, (Target.PositionProperties.Position.Y + Target.SizeProperties.Size.Y));
                    break;
                case TooltipAnchor.Bottom_Center:
                    this.Position = new Vector2(Target.PositionProperties.Position.X - Math.Abs((Target.SizeProperties.Size.X / 2 - Size.X / 2)), (Target.PositionProperties.Position.Y + Target.SizeProperties.Size.Y));
                    break;
                case TooltipAnchor.Bottom_Right:
                    this.Position = new Vector2(Target.PositionProperties.Position.X + Target.SizeProperties.Size.X, (Target.PositionProperties.Position.Y + Target.SizeProperties.Size.Y));
                    break;
            }
        }

        public TooltipAnchor Anchor
        {
            get { return this.anchor; }
            set
            {
                this.anchor = value;
                onAnchorChanged(EventArgs.Empty);
            }
        }

        public ITooltipTarget Target
        {
            get { return this.target; }
            set
            {
                this.target = value;
                onTargetChanged(EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> AnchorChanged;

        public event EventHandler<EventArgs> TargetChanged;

        protected virtual void onAnchorChanged(EventArgs e)
        {
            if (AnchorChanged != null)
            {
                AnchorChanged(this, e);
            }
        }

        protected virtual void onTargetChanged(EventArgs e)
        {
            if (TargetChanged != null)
            {
                TargetChanged(this, e);
            }
        }
    }
}
